//
//  BridgeClass.m
//  app
//
//  Created by Dordly on 2024/2/1.
//

#import "BridgeClass.h"

@implementation BridgeClass

// iOS侧方法，供ArkUI侧调用
- (NSString*)getHelloArkUI {
    return @"Hello ArkUI!";
}

- (NSString *)testBridge_NativeToTs_callMethod {
    NSString *sendMessageStr = @"success";
    [self sendMessage:sendMessageStr];
    return @"ArkUI NativeToTs success!";
}

- (NSString *)testBridge_TsToNative_callMethod:(id)params {
    NSDictionary *valueDict = [NSDictionary dictionary];
    if ([params isKindOfClass:[NSDictionary class]]) {
        valueDict = params;
    }
    NSArray *infoArray = [self sortedDictionary:[NSMutableDictionary dictionaryWithDictionary:valueDict]];
    NSString *resultStr = [infoArray componentsJoinedByString:@","];
    return resultStr;
}

- (void)testCallBackOfJs:(id)param {
    NSLog(@"testCallBackOfJs---%@", param);
}

- (void)jsMethod:(id)param {
    NSLog(@"jsMethod---%@", param);
}

- (void)jsMethod {
    NSLog(@"jsMethod");
}

- (void)testCallBackOfJs {
    NSLog(@"testCallBackOfJs");
}

- (NSString *)binary_NativeToTs_callMethod {
    NSString *sendMessageStr = @"success";
    [self sendMessage:sendMessageStr];
    return @"ArkUI NativeToTs success!";
}

- (NSString *)binary_TsToNative_callMethod:(id)params {
    NSDictionary *valueDict = [NSDictionary dictionary];
    if ([params isKindOfClass:[NSDictionary class]]) {
        valueDict = params;
    }
    NSArray *infoArray = [self sortedDictionary:[NSMutableDictionary dictionaryWithDictionary:valueDict]];
    NSString *resultStr = [infoArray componentsJoinedByString:@","];
    return resultStr;
}

- (NSString *)getCallMethod {
    return @"Hello ArkUI success";
}

- (NSString *)testBridge_sendMessage {
    return @"sendMessage Success";
}

- (NSString *)testCallBack:(id)params {
    if ([params isKindOfClass:[NSString class]]) {
        return params;
    }
    return @"CallBack Success";
}

/**
 对字典(Key-Value)排序 区分大小写
 @param dict 要排序的字典
 */
- (NSArray *)sortedDictionary:(NSMutableDictionary *)dict {
    // 将所有的key放进数组
    NSArray *allKeyArray = [dict allKeys];
    // 序列化器对数组进行排序的block 返回值为排序后的数组
    NSArray *afterSortKeyArray = [allKeyArray sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        // 排序操作
        NSComparisonResult resuest = [obj1 compare:obj2]; // 升序
        return resuest;
    }];
    // 通过排列的key值获取value
    NSMutableArray *valueArray = [NSMutableArray array];
    for (NSString *sortsing in afterSortKeyArray) {
        id valueString = [dict objectForKey:sortsing];
        if (![valueString isKindOfClass:[NSString class]]) {
            continue;
        }
        NSString *valueStr = valueString;
        if (valueStr.length < 1) {
            continue;
        }
        [valueArray addObject:valueString];
    }
    return [valueArray copy];
}



@end

